﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using mjRegister.Entity;

namespace mjRegister
{
    public partial class Msg : Form
    {
        public MsgInfo MsgInfo = new MsgInfo();
        public string Send_Msg_Str = string.Empty;
        RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);

        public Msg()
        {
            InitializeComponent();
        }

        private void Msg_Load(object sender, EventArgs e)
        {
            string msgfile = Path.Combine(System.IO.Path.GetTempPath(), "msg.html");
            if(File.GetLastWriteTime(msgfile) < new DateTime(2008,12,28))
                File.Delete(msgfile);
            if (!File.Exists(msgfile) || (File.Exists(msgfile) && File.GetLastWriteTime(msgfile).Day != DateTime.Now.Day))
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                Stream s = assembly.GetManifestResourceStream(string.Format("{0}.{1}", assembly.GetName().Name, "msg.html"));
                if (s != null)
                {
                    using (StreamReader sr = new StreamReader(s))
                    {
                        using (StreamWriter sw = new StreamWriter(msgfile, false, Encoding.UTF8))
                        {
                            sw.Write(sr.ReadToEnd());
                        }
                    }
                }
            }

            this.webBrowser1.Navigate(msgfile);
            this.webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
            this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            HtmlDocument doc = ((WebBrowser)sender).Document;
            doc.InvokeScript("content_editor_init", new object[] { Send_Msg_Str });
        }

        void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            string url = e.Url.ToString();
            if (url.StartsWith("http://msg/"))
            {
                e.Cancel = true;

                string msg = url.Replace("http://msg/?", string.Empty);

                string re = "content=(?<content>.*?)&texttype=(?<texttype>.*?)";

                if (Regex.IsMatch(msg, re, options))
                {
                    Match match = Regex.Match(msg, re, options);

                    MsgInfo = new MsgInfo
                    {
                        content = match.Result("${content}"),
                        texttype = match.Result("${texttype}")
                    };
                }
                this.DialogResult = DialogResult.OK;

                this.Close();
            }
        }

    }
}
